#include "StdAfx.h"
#include "xhImgAnaly.h"
#include "xhImgAnaly.h"
#include "DDFun.h"
#include "DDReadWriteFile.h"

#ifdef DKQ_TEST
DDLogInfo g_xLogInfo;
#endif

xhImgAnaly::xhImgAnaly()
{
	m_dwContext       = 0;
	m_pContext        = NULL;
	m_funMsgCallBack  = NULL;

	m_nTemplateCount = 0;

	m_nThreadType = eThdType_Nothing;

#ifdef DKQ_TEST
	g_xLogInfo.CreateLogDir("ImgAnaly",0x1000);
#endif

#ifdef DKQ_ADD_LOG_FILE
	sprintf(m_szLogFile,"");
#endif
}

xhImgAnaly::~xhImgAnaly()
{
	UnInitialize();
}

int xhImgAnaly::UnInitialize()
{
	DDThread3::DDThdUnInit();

	DD_AUTO_LOCK xLock(&m_Sect);

	xkImageAnaly::UnInitialize();
	xkRefPointMan::UnInitialize();

	return 0;
}


int xhImgAnaly::Initialize()
{
	xkImageAnaly::Initialize();
	m_xDispRef.SetDisplayPalette(0,65535,16);
	m_xDispSap.SetDisplayPalette(0,65535,16);
	m_xDispAdj.SetDisplayPalette(0,65535,16);
	m_xDispSub.SetDisplayPalette(0,65535,16);

	xkRefPointMan::Initialize();

	DDThread3::DDThdInit();

	return DD_ERR_SUCCESS;
}

int xhImgAnaly::SetHwnd(int nWindow,HWND hWnd,zkScreenMon *pScreen)
{
	switch( nWindow )
	{
	case eAnalyWindow_Ref:
		{
			m_xDispRef.SetParent( hWnd,pScreen );
		}
		break;
	case eAnalyWindow_Sap:
		{
			m_xDispSap.SetParent( hWnd,pScreen );
		}
		break;
	case eAnalyWindow_Adj:
		{
			m_xDispAdj.SetParent( hWnd,pScreen );
		}
		break;
	case eAnalyWindow_Sub:
		{
			m_xDispSub.SetParent( hWnd,pScreen );
		}
		break;
	default:
		break;
	}

	return DD_ERR_SUCCESS;
}

int xhImgAnaly::RegOnDrawCallBack(AnalyOnDrawCallBackFun funCallBack,void *pContext)
{
	int nRet = 0;
	nRet |= m_xDispRef.RegOnDrawCallBack(funCallBack,eAnalyWindow_Ref,pContext);
	nRet |= m_xDispSap.RegOnDrawCallBack(funCallBack,eAnalyWindow_Sap,pContext);
	nRet |= m_xDispAdj.RegOnDrawCallBack(funCallBack,eAnalyWindow_Adj,pContext);
	nRet |= m_xDispSub.RegOnDrawCallBack(funCallBack,eAnalyWindow_Sub,pContext);
	return nRet;
}

int xhImgAnaly::RegMsgCallBack(AnalyMsgCallBackFun funCallBack,void *pContext)
{
    DD_AUTO_LOCK xLock(&m_Sect);

	m_dwContext       = 0;
	m_pContext        = pContext;
	m_funMsgCallBack  = funCallBack;

	return 0;
}


int xhImgAnaly::DeleteTemplate(int nType)
{
	if( nType == m_nTempType )
	{
		xkRefPointMan::UnInitialize();
	}
	
	char szTempPath[MAX_PATH] = "";
	sprintf(szTempPath,"%s\\%03d\\",m_szAnalyDir,m_nTempType);

	DDDeleteDeepDir( szTempPath );

	return 0;
}

int xhImgAnaly::GetTemplates()
{
	m_nTemplateCount = 0;
	m_nThreadType    = eThdType_LoadTemplates;
	DDThread3::SetRunEvent();

	return 0;
}

void xhImgAnaly::Thread_LoadTemplates()
{
	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_LoadTemplates,AnalyMsgState_Begin,0,0,m_pContext);
	}

	int              nCount = 0;
	zkAnalyTempInfo *pTemps = NULL;


	char *pTmp = NULL;
	char szTempFile[MAX_PATH] = "";
	char szTempPath[MAX_PATH] = "";
	sprintf(szTempPath,"%s\\*.*",m_szAnalyDir);

	int           nRet = DD_ERR_FAILURE;

	DDReadWriteFile xFile;
	LONGLONG        llFileSize = 0;
	LONGLONG        llFileOffset;
	zkAnalyFileTail xFileTail;

	WIN32_FIND_DATA   filedata; 
	HANDLE            filehandle; 

	filehandle = FindFirstFile( szTempPath,&filedata ); 
	if( INVALID_HANDLE_VALUE != filehandle ) 
	{ 
		do 
		{ 
			if( FILE_ATTRIBUTE_DIRECTORY == filedata.dwFileAttributes )
			{
				if( strlen(filedata.cFileName)>0 && filedata.cFileName[0] != '.' ) 
				{ 
					sprintf(szTempFile,"%s\\%s\\000.%s",m_szAnalyDir,filedata.cFileName,DD_ANALYTEMPEXT);
					
					do 
					{
						llFileSize = 0;
						if( 0 != xFile.DDCreateFile(szTempFile,TRUE,&llFileSize) )
						{
							break;
						}

						llFileOffset = llFileSize - sizeof(zkAnalyFileTail);
						nRet = xFile.DDOperatorFile( (BYTE*)&xFileTail,sizeof(zkAnalyFileTail),&llFileOffset);
						if( nRet != 0)
						{
							break;
						}
						if( !xFileTail.CheckVersion() )
						{
							break; 
						}

						m_xTemplateInfos[nCount].nTempType = atoi(filedata.cFileName);
						sprintf(m_xTemplateInfos[nCount].szTempName,"%.30s",xFileTail.szAnalyTempName);
                        nCount++;

					}while(false);
				} 
			}

			if( nCount >= MAX_TEMP_COUNT )
			{
				break;
			}
			
		}while( FindNextFile(filehandle,&filedata) ); 

		FindClose(filehandle); 
	} 

	if( nCount > 0 )
	{
		m_nTemplateCount = nCount;
		pTemps           = m_xTemplateInfos;
	}
	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_LoadTemplates,AnalyMsgState_Finish,pTemps,nCount*sizeof(zkAnalyTempInfo),m_pContext);
	}
}

int xhImgAnaly::GetTemplateList(void **ppList,int &nCount)
{
	if( m_nTemplateCount <= 0 )
	{
		Thread_LoadTemplates();
	}

	if( m_nTemplateCount > 0 )
	{
		*ppList = m_xTemplateInfos;
		nCount  = m_nTemplateCount;

		return DD_ERR_SUCCESS;
	}
	
	return DD_ERR_FAILURE;
}

int xhImgAnaly::SelectTemplate(int nType,char *pszName,BOOL bBlock)
{
	if( nType >= 0 )
	{
	}
	else if( NULL != pszName )
	{
		for(int i=0;i<m_nTemplateCount;i++)
		{
			if( strcmp(pszName,m_xTemplateInfos[i].szTempName) == 0 )
			{
				nType = m_xTemplateInfos[i].nTempType;
				break;
			}
		}
	}

	if( m_nTempType == nType )
	{
		return DD_ERR_SUCCESS;
	}
	m_nTempType = nType;

	if( bBlock )
	{
		Thread_LoadTempPoints(FALSE);
	}
	else
	{
		GetTemplates();
	}
	
	return DD_ERR_SUCCESS;
}

int xhImgAnaly::GetSelectTemplate(zkAnalyTempInfo **ppInfo)
{
	*ppInfo = NULL;

	for(int i=0;i<m_nTemplateCount;i++)
	{
		if( m_nTempType == m_xTemplateInfos[i].nTempType )
		{
			*ppInfo = &( m_xTemplateInfos[i] );
			
			return DD_ERR_SUCCESS;
		}
	}

	return DD_ERR_FAILURE;
}

int xhImgAnaly::SetTemplate(int nType,zkAnalyTempInfo *pInfo)
{
	int nRet = xkRefPointMan::SetTemplate( nType,pInfo);
	if( DD_ERR_SUCCESS == nRet )
	{
		if( NULL != m_funMsgCallBack )
		{
			m_funMsgCallBack(AnalyMsg_LoadTempPoints,AnalyMsgState_Finish,0,0,m_pContext);
		}  
		return nRet;
	}

	m_nThreadType = eThdType_LoadTempPoints;
	DDThread3::SetRunEvent();

	return 0;
}
inline void xhImgAnaly::Thread_LoadTempPoints(BOOL bCallBack)
{
	DD_AUTO_LOCK xLock(&m_Sect);

	if( NULL != m_funMsgCallBack && bCallBack )
	{
		m_funMsgCallBack(AnalyMsg_LoadTempPoints,AnalyMsgState_Begin,0,0,m_pContext);
	}

#ifdef DKQ_TEST_TEMP_TIME
	g_xLogInfo.WriteLog(__FILE__,__LINE__,"LoadTemplate ------");
#endif
	xkRefPointMan::LoadTempPoints();
#ifdef DKQ_TEST_TEMP_TIME
	g_xLogInfo.WriteLog(__FILE__,__LINE__,"LoadTemplate ++++++");
#endif

	if( NULL != m_funMsgCallBack && bCallBack )
	{
		m_funMsgCallBack(AnalyMsg_LoadTempPoints,AnalyMsgState_Finish,0,0,m_pContext);
	}  
}







void xhImgAnaly::SetDefaultDispPalette(int nWindow,zkImgBitInfo *pImgInfo)
{
	int nDark   = 0;
	int nBright = 65535;
	int nBpp    = 16;
	
	nBpp = pImgInfo->nBitPerSample;
	nBright = (1<<nBpp) - 1;

	switch( pImgInfo->nImgDefine ) 
	{
	case eImage_Define_Img8:
		{
			
		}
		break;
	case eImage_Define_Img16:
		{
			 
		}
		break;
	case eImage_Define_Img32:
		{
		}
		break;
	default:
		break;
	}

	switch( nWindow )
	{
	case eAnalyWindow_Ref:
		{
			m_xDispRef.SetDisplayPalette(nDark,nBright,nBpp);
		}
		break;
	case eAnalyWindow_Sap:
		{
			m_xDispSap.SetDisplayPalette(nDark,nBright,nBpp);
		}
		break;
	case eAnalyWindow_Adj:
		{
			m_xDispAdj.SetDisplayPalette(nDark,nBright,nBpp);
		}
		break;
	case eAnalyWindow_Sub:
		{
			m_xDispSub.SetDisplayPalette(nDark,nBright,nBpp);
		}
		break;
	default:
		break;
	}
}

int xhImgAnaly::LoadRefDataToDisp()
{
	m_nThreadType = eThdType_RefDisplay;
	DDThread3::SetRunEvent();

	return 0;
}

inline void xhImgAnaly::Thread_LoadRefDataToDisp()
{
	DD_AUTO_LOCK xLock(&m_Sect);
	
	int nRet = -1;

	if( NULL != xkRefPointMan::m_pActivePoint )
	{
		zkImgBitInfo *pImgInfo = NULL;
		nRet = xkRefPointMan::m_pActivePoint->GetOrgImgBuffer( &pImgInfo );
		if( DD_ERR_SUCCESS == nRet )
		{
			zkAnalyMaskHead *pHead = &(xkRefPointMan::m_pActivePoint->m_xMaskHead);
			if( NULL != pHead->pMask )
			{
				nRet = m_xDispRef.SetDisplayData2( pImgInfo,pHead,&m_xRefDraw );
			}
			else
			{
				nRet = m_xDispRef.SetDisplayData( pImgInfo,&m_xRefDraw );
			}
		}
	}
}




int xhImgAnaly::DDThdDispatch(HANDLE hExitEvent)
{
	try
	{
		switch( m_nThreadType )
		{
		case eThdType_LoadTemplates:  Thread_LoadTemplates();     break;
		case eThdType_LoadTempPoints: Thread_LoadTempPoints();    break;
		case eThdType_RefDisplay:     Thread_LoadRefDataToDisp(); break;
		case eThdType_SubDisplay:     Thread_LoadSubDataToDisp(); break;
		case eThdType_ImageAdjEdge:   Thread_ImageAdjEdge();      break;
		case eThdType_ImageSub:       Thread_ImageSub();          break;
		case eThdType_ImageDelEdge:   Thread_ImageDelEdge();      break;
		case eThdType_ImageParse:     Thread_ImageParse();        break;
		case eThdType_ImageAll:       Thread_ImageAll();          break;
		case eThdType_ImageAnaly:     Thread_ImageAnaly();        break;
		case eThdType_AddSapToAidRef: Thread_AddSapToAidRef();    break;
		default:break;
		}
	}
	catch (...)
	{
	}

	m_nThreadType = eThdType_Nothing;
	DDThread3::SetRunEvent(false);

	return 0;
}





int xhImgAnaly::GetImageSize(int &nWidth,int &nHeight)
{
	//DD_AUTO_LOCK xLock(&m_Sect);

	int nRet = -1;

	if( NULL != xkRefPointMan::m_pActivePoint )
	{
		nRet = xkRefPointMan::m_pActivePoint->GetImageSize(nWidth,nHeight);
	}

    return nRet;
}



int	xhImgAnaly::Draw(int nWindow,void *pVoid)
{
	//DD_AUTO_LOCK xLock(&m_Sect);

	int nRet = 0;

	switch( nWindow )
	{
	case eAnalyWindow_Ref:
		{
			if( NULL != pVoid )
			{
				memcpy(&m_xRefDraw,pVoid,sizeof(m_xRefDraw));
			}

			nRet = m_xDispRef.SetDisplayDst(pVoid);
		}
		break;
	case eAnalyWindow_Sap:
		{
			if( NULL != pVoid )
			{
				memcpy(&m_xSapDraw,pVoid,sizeof(m_xSapDraw));
			}

			nRet |= m_xDispSap.SetDisplayDst(pVoid);
		}
		break;
	case eAnalyWindow_Adj:
		{
			if( NULL != pVoid )
			{
				memcpy(&m_xSapDraw,pVoid,sizeof(m_xSapDraw));
			}

			nRet |= m_xDispAdj.SetDisplayDst(pVoid);
		}
		break;
	case eAnalyWindow_Sub:
		{
			if( NULL != pVoid )
			{
				memcpy(&m_xSapDraw,pVoid,sizeof(m_xSapDraw));
			}
			
			nRet |= m_xDispSub.SetDisplayDst(pVoid);
		}
		break;
	default:
		break;
	}

	return nRet;
}

int xhImgAnaly::ImageCtrl(int nWindow,DWORD dwType,void *pVoid)
{
	DD_AUTO_LOCK xLock(&m_Sect);

	int nRet = 0;

	switch( dwType )
	{
	case ImageCtrl_ClearScreen:
		switch( nWindow )
		{
		case eAnalyWindow_Ref: nRet = m_xDispRef.ClearScreen(0x07); break;
		case eAnalyWindow_Sap: nRet = m_xDispSap.ClearScreen(0x07); break;
		case eAnalyWindow_Adj: nRet = m_xDispAdj.ClearScreen(0x07); break;
		case eAnalyWindow_Sub: nRet = m_xDispSub.ClearScreen(0x07); break;
		default:break;
		}
		break;
	case ImageCtrl_Histogram:
		{
			if( NULL == pVoid ) 
			{
				nRet = DD_ERR_PARAMETER;
				break;
			}

			int *pTemp = (int *)pVoid; 

			switch( nWindow )
			{
			case eAnalyWindow_Ref:
				{
					int nLeft  = *pTemp;
					int nRight = *(pTemp+1);
					int nBpp   = m_xDispRef.m_nBpp;

					nRet = m_xDispRef.SetDisplayPalette(nLeft,nRight,nBpp);

					LoadRefDataToDisp();
				}
				break;
			case eAnalyWindow_Sub:
				{
					int n1 = *pTemp;
					int n2 = *(pTemp+1);
					int nBpp  = m_xDispSub.m_nBpp; 

					nRet = m_xDispSub.SetSubDisplayPalette2(n1,n2,nBpp);

					LoadSubDataToDisp();
				}
				break;
			case eAnalyWindow_Sub_AdjEdge:
				{
					int n1 = *pTemp;
					int nBpp  = m_xDispSub.m_nBpp; 

					nRet = m_xDispSub.SetDisplayPalette(n1,n1,nBpp);
					LoadSubDataToDisp();
				}
				break;
			default:
				break;
			}
		}
		break;
	case ImageCtrl_ImgBinary:
		{
			if( NULL == pVoid ) 
			{
				nRet = DD_ERR_PARAMETER;
				break;
			}

			int *pTemp = (int *)pVoid; 

			switch( nWindow )
			{
			case eAnalyWindow_Ref:
				{
					int n1 = *pTemp;
					int nBpp  = m_xDispSub.m_nBpp; 

					nRet = m_xDispRef.SetDisplayPalette(n1,n1,nBpp);

					LoadRefDataToDisp();
				}
				break;
			case eAnalyWindow_Sub:
				{
					int n1 = *pTemp;
					int nBpp  = m_xDispSub.m_nBpp; 

					nRet = m_xDispSub.SetDisplayPalette(n1,n1,nBpp);
					LoadSubDataToDisp();
				}
				break;
			default:
				break;
			}
		}
		break;
	default:
		break;
	}

	return nRet;
}




int xhImgAnaly::LoadSubDataToDisp()
{
	m_nThreadType = eThdType_SubDisplay;
	DDThread3::SetRunEvent();

	return 0;
}

inline void xhImgAnaly::Thread_LoadSubDataToDisp()
{
	DD_AUTO_LOCK xLock(&m_Sect);

	int nRet = -1;

	if( NULL != xkImageAnaly::m_pImgBit )
	{
		zkImgBitInfo *pImgInfo = NULL;
		nRet = xkImageAnaly::m_pImgBit->GetImgBuffer( &pImgInfo );
		if( DD_ERR_SUCCESS == nRet )
		{
			nRet = m_xDispSub.SetDisplayData( pImgInfo,&m_xSapDraw );
		}
	}
}



int xhImgAnaly::GetHistogramInfo(int nWindow,zkImgHistogram **ppHistogram)
{
	if( eAnalyWindow_Ref == nWindow )
	{
        if( NULL != xkRefPointMan::m_pActivePoint )
		{
			return xkRefPointMan::m_pActivePoint->m_pImgBit->GetHistogramInfo(nWindow,ppHistogram);
		}
	}
	else
	{
		DD_AUTO_LOCK xLock(&m_Sect);
		if( NULL == xkImageAnaly::m_pImgBit )
		{
			return DD_ERR_FAILURE;
		}

		int nRet = xkImageAnaly::m_pImgBit->GetHistogramInfo(nWindow,ppHistogram);
		if( DD_ERR_SUCCESS == nRet )
		{
			if( eAnalyWindow_Sub_AdjEdge == nWindow )
			{
                int nImgBinary = xkImageAnaly::m_pClosetRefPoint->m_xEdgeHead.dwImgBinary;
				if( nImgBinary > 0 )
				{
					(*ppHistogram)->nDark   = nImgBinary;
					(*ppHistogram)->nBright = nImgBinary;
				}
			}
			else if( eAnalyWindow_Sub == nWindow )
			{
				int nDark   = xkImageAnaly::m_pClosetRefPoint->m_xEdgeHead.dwMaxRed;
				int nBright = xkImageAnaly::m_pClosetRefPoint->m_xEdgeHead.dwMinBlue;

				if( nDark >= 0 && nBright > 0 )
				{
					(*ppHistogram)->nDark   = nDark;
					(*ppHistogram)->nBright = nBright;
				}
			}
		}

		return nRet;
	}
	return DD_ERR_FAILURE;
}



int xhImgAnaly::CheckPoint(int nPoint,zkImgBitInfo *pImgInfo,BOOL bShareMem,BOOL bShowImg)
{
	if( NULL == pImgInfo )
	{
		return DD_ERR_PARAMETER;
	}

	int nRet = 0;

	DD_AUTO_LOCK xLock(&m_Sect);

	do 
	{
		nRet = xkRefPointMan::SelectPoint(nPoint);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

		nRet = xkImageAnaly::CheckSapRefSize( pImgInfo );
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

		nRet = xkImageAnaly::SetChkBuffer(pImgInfo,bShareMem);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

	}while(false);

	return nRet;
}


int xhImgAnaly::OperateMethod(DWORD dwMethod,void *pVoid)
{
	int nRet = 0;

//Ref////////////
	switch( dwMethod )
	{
	case eAnalyMethod_AddMask:
		{
			nRet = xkRefPointMan::SetMask( pVoid );
			LoadRefDataToDisp();

			return 0;
		}
		break;
	case eAnalyMethod_DelMask:
		{
			nRet = xkRefPointMan::DelMask( pVoid );
			LoadRefDataToDisp();

			return 0;
		}
		break;
	case eAnalyMethod_EdgeConditionThreshold: 
		{
			float f = *( (float*)pVoid );
			nRet = xkRefPointMan::SetEdgeConditionThreshold(f);
		}
		break;
	default:  break;
	}
///////////////////////////////////

	if( NULL == m_pImgBit )
	{
		return DD_ERR_FAILURE;
	}

	switch( dwMethod )
	{
	case eAnalyMethod_Adj:             OperateImageAdjEdge();  break;  
	case eAnalyMethod_Sub:             OperateImageSub();      break;             
	case eAnalyMethod_DelEdge:         OperateImageDelEdge();  break;
	case eAnalyMethod_Parse:           OperateImageParse();    break;
	case eAnalyMethod_AddSapToAidRef:  OperateAddSapToAidRef();break;
	case eAnalyMethod_AddAdjEdgeToRef:
		{
			if( NULL == pVoid ) return DD_ERR_PARAMETER;

			int *pTemp = (int *)pVoid; 
			int n1 = *pTemp;

			DD_AUTO_LOCK xLock(&m_Sect);
			nRet = xkImageAnaly::AddAdjEdgeToRef(n1);  
		}
		break;
	case eAnalyMethod_AddSubEdgeToRef:   
		{
			if( NULL == pVoid ) return DD_ERR_PARAMETER;

			int *pTemp = (int *)pVoid; 
			int n1 = *pTemp;
			int n2 = *(pTemp+1);

			DD_AUTO_LOCK xLock(&m_Sect);
			nRet = xkImageAnaly::AddSubEdgeToRef(n1,n2);  
		}
		break;
	case eAnalyMethod_DelSubDefect:
		{
            nRet = m_pImgBit->DelSubDefect( pVoid );
			LoadSubDataToDisp();
		}
		break;
	case eAnalyMethod_RestorSubDefect:
		{
			nRet = m_pImgBit->RestorSubDefect( pVoid );
			LoadSubDataToDisp();
		}
		break;
	default:  break;
	}

	return nRet;
}

int xhImgAnaly::OperateImageAdjEdge()
{
	DD_AUTO_LOCK xLock(&m_Sect);
	m_nThreadType = eThdType_ImageAdjEdge;
	DDThread3::SetRunEvent();

	return 0;
}
int xhImgAnaly::OperateImageSub()
{
	DD_AUTO_LOCK xLock(&m_Sect);
	m_nThreadType = eThdType_ImageSub;
	DDThread3::SetRunEvent();

	return 0;
}
int xhImgAnaly::OperateImageDelEdge()
{
	DD_AUTO_LOCK xLock(&m_Sect);
	m_nThreadType = eThdType_ImageDelEdge;
	DDThread3::SetRunEvent();

	return 0;
}
int xhImgAnaly::OperateImageParse()
{
	DD_AUTO_LOCK xLock(&m_Sect);
	m_nThreadType = eThdType_ImageParse;
	DDThread3::SetRunEvent();

	return 0;
}
int xhImgAnaly::OperateImageAll()
{
	DD_AUTO_LOCK xLock(&m_Sect);
	m_nThreadType = eThdType_ImageAll;
	DDThread3::SetRunEvent();

	return 0;
}
int xhImgAnaly::OperateImageAnaly()
{
	DD_AUTO_LOCK xLock(&m_Sect);
	m_nThreadType = eThdType_ImageAnaly;
	DDThread3::SetRunEvent();

	return 0;
}
inline void xhImgAnaly::Thread_ImageAdjEdge()
{
	DD_AUTO_LOCK xLock(&m_Sect);

	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_AdjImageEdge,AnalyMsgState_Begin,0,0,m_pContext);
	}

	xkImageAnaly::ChkOperate_AdjEdge();
}
inline void xhImgAnaly::Thread_ImageSub()
{
	DD_AUTO_LOCK xLock(&m_Sect);
	
	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_SubImage,AnalyMsgState_Begin,0,0,m_pContext);
	}

	xkImageAnaly::ChkOperate_Sub();
}

inline void xhImgAnaly::Thread_ImageDelEdge()
{
	DD_AUTO_LOCK xLock(&m_Sect);

	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_DelEdge,AnalyMsgState_Begin,0,0,m_pContext);
	}

	xkImageAnaly::ChkOperate_DelEdge();
}
inline void xhImgAnaly::Thread_ImageParse()
{
	DD_AUTO_LOCK xLock(&m_Sect);

	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_ParseImage,AnalyMsgState_Begin,0,0,m_pContext);
	}

	xkImageAnaly::ChkOperate_Parse();
}
inline void xhImgAnaly::Thread_ImageAll()
{
	DD_AUTO_LOCK xLock(&m_Sect);

	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_ParseImage,AnalyMsgState_Begin,0,0,m_pContext);
	}

	xkImageAnaly::ChkOperate_All();
}
inline void xhImgAnaly::Thread_ImageAnaly()
{
	DD_AUTO_LOCK xLock(&m_Sect);

	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_Result,AnalyMsgState_Begin,0,0,m_pContext);
	}

	xkImageAnaly::DstOperate_Analy();
}




int xhImgAnaly::OperateAddSapToAidRef()
{
	DD_AUTO_LOCK xLock(&m_Sect);
	m_nThreadType = eThdType_AddSapToAidRef;
	DDThread3::SetRunEvent();
	return 0;
}
inline void xhImgAnaly::Thread_AddSapToAidRef()
{
	DD_AUTO_LOCK xLock(&m_Sect);

	if( NULL != m_funMsgCallBack )
	{
		m_funMsgCallBack(AnalyMsg_AddSapToAidRef,AnalyMsgState_Begin,0,0,m_pContext);
	}

	xkImageAnaly::AddSapToAidRef(); 
}


int xhImgAnaly::SetLogFile(char *pLogFile)
{
#ifdef DKQ_ADD_LOG_FILE
	sprintf(m_szLogFile,pLogFile);
#endif

	return 0;
}

int xhImgAnaly::SetImgBuffer(int nPoint,zkImgBitInfo *pImgInfo,BOOL bShareMem,BOOL bBlock)
{
	if( NULL == pImgInfo )
	{
		return DD_ERR_PARAMETER;
	}

	int nRet = 0;

	DD_AUTO_LOCK xLock(&m_Sect);

#ifdef DKQ_TEST_ANALY
	g_xLogInfo.WriteLog(__FILE__,__LINE__,"SetImgBuffer");
#endif

	do 
	{
		nRet = xkRefPointMan::SelectPoint(nPoint);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

#ifdef DKQ_TEST_ANALY
		g_xLogInfo.WriteLog(__FILE__,__LINE__,"start point: %d",nPoint);
#endif
		nRet = xkImageAnaly::CheckSapRefSize( pImgInfo );
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

		nRet = xkImageAnaly::SetDstBuffer(pImgInfo,bShareMem,bBlock);
		if( DD_ERR_SUCCESS != nRet )
		{
			break;
		}

#ifdef DKQ_TEST_ANALY
		g_xLogInfo.WriteLog(__FILE__,__LINE__,"start point: %d",nPoint);
#endif


	}while(false);

	return nRet;
}






